<?php
declare(strict_types=1);


namespace App\Marketing\Mapper;

use App\Mall\Model\MallGoods;
use App\Marketing\Model\MarketingGradient;
use Hyperf\Database\Model\Builder;
use Mine\Abstracts\AbstractMapper;

/**
 * 梯度消费奖励Mapper类
 */
class MarketingGradientMapper extends AbstractMapper
{
    /**
     * @var MarketingGradient
     */
    public $model;

    public function assignModel()
    {
        $this->model = MarketingGradient::class;
    }

    /**
     * 搜索处理器
     * @param Builder $query
     * @param array $params
     * @return Builder
     */
    public function handleSearch(Builder $query, array $params): Builder
    {

        // 主键
        if (isset($params['id']) && filled($params['id'])) {
            $query->where('id', '=', $params['id']);
        }

        // 0默认1商品营销
        if (isset($params['type']) && filled($params['type'])) {
            $query->where('type', '=', $params['type']);
        }

        // 标识
        if (isset($params['code']) && filled($params['code'])) {
            $query->where('code', '=', $params['code']);
        }

        return $query;
    }

    public function objectData(?string $event): array
    {
        if ($event == 'AccountOrderPayEvent') {
            return MallGoods::query()->select(['id', 'name'])->orderByDesc('sort')->get()->map(function ($item) {
                $item->name = fastapp_lang_text($item->name);
                return $item;
            })->toArray();
        }
        return [];
    }
}